{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# ------------------------------------------------------------------------------\n",
    "## [Description]\n",
    "### This function calculates the fold-change and p-value of two groups.\n",
    "## [Input]\n",
    "### dataframe -> the grouped data; groupVector_a/b --> a vector of groupA that represent the column index;\n",
    "## [Output]\n",
    "### returns the input dataframe with two additional(new) columns: 'fold_change', 'p_value'\n",
    "## [Details]\n",
    "### When calculating the p-value, this function use apply() to get the results\n",
    "## [Notes]\n",
    "### This function is faster than get_Ttest_Result_2()\n",
    "# ------------------------------------------------------------------------------\n",
    "get_Ttest_Result_2 <- function(dfrm, groupVector_a, groupVector_b){\n",
    "    # Get the average value of sample group A (non-smokers) & group B (smokers)\n",
    "    non_smoker_mean <- apply(dfrm[, groupVector_a], 1, mean)\n",
    "    smoker_mean <- apply(dfrm[, groupVector_b], 1, mean)\n",
    "    # Calculate the fold-change\n",
    "    fold_change <- smoker_mean / non_smoker_mean\n",
    "    fold_change_dfrm <- as.data.frame(fold_change)\n",
    "\n",
    "    # Use apply() to apply t.test and get results of p-value\n",
    "    ## Use tempFunction to get p-value\n",
    "    p_value <- apply(dfrm[1:(dim(dfrm)[1]-1),], 1, function(x) t.test(as.numeric(x[groupVector_a]),as.numeric(x[groupVector_b]))$p.value)\n",
    "    p_value <- append(p_value, NA) # Handle the NA value\n",
    "    p_value_dfrm <- as.data.frame(p_value)\n",
    "\n",
    "    # Add two new columns to the input dfrm\n",
    "    dfrm <- cbind(dfrm, fold_change_dfrm, p_value_dfrm)\n",
    "    # Return Value\n",
    "    return(dfrm)\n",
    "}\n",
    "                     \n",
    "\n",
    "dfrm <- read.csv('C:\\\\Users\\\\Nature\\\\Desktop\\\\M_RLanguage\\\\Lab_6\\\\GSE5056_series_matrix.txt',\n",
    "                header = TRUE, sep='\\t', row.names=1)\n",
    "dfrm_new = na.omit(get_Ttest_Result_2(dfrm, 1:18, 19:44))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "co_p <- 2 # cutoff of p_value: 0.01 (-log10(0.01) = 2)\n",
    "co_f <- 0.35 # cutoff of fold_change: +-1.274561(log2(cutoff) = +-0.35)\n",
    "dfrm_new$Expression = \"Normal\"\n",
    "dfrm_new[(dfrm_new$fold_change > 2^co_f) & (dfrm_new$p_value < 10^-co_p),]['Expression']='Up'\n",
    "dfrm_new[(dfrm_new$fold_change < 2^-co_f) & (dfrm_new$p_value < 10^-co_p),]['Expression']='Down'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<strong>png:</strong> 2"
      ],
      "text/latex": [
       "\\textbf{png:} 2"
      ],
      "text/markdown": [
       "**png:** 2"
      ],
      "text/plain": [
       "png \n",
       "  2 "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pdf(\"Boxplot_Plot_of_GSE5056_zzf_0605.pdf\")\n",
    "# 底部 左部 顶部 右边\n",
    "par(mfrow=c(1,2),mar=c(8,9,6,3),bg='#FFFFF0',tcl=0,cex.axis=0.5,mex=0.24)\n",
    "boxplot(log2(dfrm_new[dfrm_new$Expression == 'Up',][,1:44])+1,\n",
    "        horizontal=TRUE, cex=0.5,las=2,col=topo.colors(44),border=\"#708090\", pch=16,\n",
    "       xlab=\"log2(Up Expression)+1\",yaxt = \"n\",ylab=\"Example\")\n",
    "boxplot(log2(dfrm_new[dfrm_new$Expression == 'Down',][,1:44])+1,\n",
    "        horizontal=TRUE, cex=0.5,las=2,col=topo.colors(44),border=\"#708090\",pch=16,\n",
    "       xlab=\"log2(Down Expression)+1\")\n",
    "mtext(\"BoxPlot of Expression\", side = 3, line = -4, outer = TRUE)\n",
    "# mtext(\"Example\", side = 3, line = -55, outer = TRUE,)\n",
    "dev.off()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "R",
   "language": "R",
   "name": "ir"
  },
  "language_info": {
   "codemirror_mode": "r",
   "file_extension": ".r",
   "mimetype": "text/x-r-source",
   "name": "R",
   "pygments_lexer": "r",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
